/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.jabber.tree;

import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Mode;
import org.json.JSONObject;
import org.webos.java.base.tree.TreeController;
import org.webos.java.base.tree.TreeControllerItem;

/**
 *
 * @author Kostya
 */
public class UserItem extends TreeControllerItem<RosterEntry, UserItem>{

    private Roster roster = null;
    String userName = "";
    String userJID = "";
    private Presence presence;
    int status = ConnectionItem.STATUS_OFFLINE;

    public UserItem(TreeController aController, String aUserName, RosterEntry aObject, Roster aRoster) {
        super(aController, aObject);
        getData().putOpt("draggable", true);
        userName = aUserName;
        roster = aRoster;
        setId(aUserName+"-u"+aObject.getUser());
        setType(TYPE_LEAF);
        updateInfo();
    }

    public String getUserName() {
        return userName;
    }

    static String getEntryName(Roster aRoster, String aEntry){
        RosterEntry entry = aRoster.getEntry(aEntry);
        if(entry==null)
            return aEntry;
        if(entry.getName()==null || "".equals(entry.getName()))
            return aEntry;
        return entry.getName();
    }

    static int getStatus(Roster aRoster, RosterEntry aEntry){
        return getStatus(aRoster, aEntry.getUser());
    }
    
    public static int getStatus(Roster aRoster, String aEntry){
        Presence presence = aRoster.getPresence(aEntry);
        int st = ConnectionItem.STATUS_OFFLINE;
        if(presence.getType().equals(Presence.Type.available)){
            if(Mode.away.equals(presence.getMode())){
                st = ConnectionItem.STATUS_AWAY;
            }else{
                if(Mode.xa.equals(presence.getMode())){
                    st = ConnectionItem.STATUS_XA;
                }else{
                    if(Mode.dnd.equals(presence.getMode())){
                        st = ConnectionItem.STATUS_DND;
                    }else{
                        st = ConnectionItem.STATUS_ONLINE;
                    }
                }
            }
        }else{
            st = ConnectionItem.STATUS_OFFLINE;
        }

        return st;
    }

    private void updateInfo(){
        presence = roster.getPresence(getObject().getUser());
        userJID = getObject().getUser();
        if(null==getObject().getName())
            userName = userJID;
        else
            userName = getObject().getName();
        status = getStatus(roster, getObject());
    }

    @Override
    public void notifyModified() {
        updateInfo();
        super.notifyModified();
    }



    @Override
    public void objectTOJSON(JSONObject aObject) {
//        Presence p = roster.getPresence(getObject().getUser());
        if(presence==null){
            updateInfo();
        }
        aObject.putOpt("icon_cls", "jabber_icn jabber_"+status);
        String result = "";

        result += userName;
        //"["+p.getMode()+"]["+(p.getStatus()==null?"":p.getStatus())+"]"
        if(presence.getStatus()!=null && !"".equals(presence.getStatus())){
            result += "("+presence.getStatus().replaceAll("\n", " ")+")";
        }
        aObject.putOpt("name", result);
    }

    public String getUserJID() {
        return userJID;
    }

    public int getCurrentStatus() {
        return status;
    }


}
